﻿using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        private void YGRAB50DOC(DataSet db)
        {
            string value = "";
            DataRow dwMaster = null;
            DataRow dwABK = null;
            DataRow dwWS = null;
            int rStart = 0;
            int nRow = 1;
            int nPRow = 1;
            int idx = 0;
            int pages = 0;
            string KRS, LFD, POS;
            dwMaster = db.Tables["ABPOS"].Rows[0];
            dwABK = db.Tables["ABKOPF"].Rows[0];
           KRS = dwMaster["ABPAUFKRS"].ToString();
           LFD = dwMaster["ABPAUFLFD"].ToString();
           POS= dwMaster["ABPAUFPOS"].ToString();
            /*  
            if (db.Tables["WASTAMM"].Rows.Count > 0)
              {
                  dwWS = db.Tables["WASTAMM"].Rows[0];
              }*/
            string art = dwMaster["ABPART"].ToString();
            string var = dwMaster["ABPVAR"].ToString();
            string color = dwMaster["ABPFARBE"].ToString();
            #region====Header======
            exAgent.SetCellValue(3, 29, date);
            exAgent.SetCellValue(1, 23, "*" + KRS + LFD + POS + "*");//barcode

            exAgent.SetCellValue(3, 5, dwMaster["ABPAUFKRS"].ToString() + "-" + Helpers.Helper.GetSubRollNum(dwMaster["ABPAUFLFD"].ToString())
                + "-" + Helpers.Helper.GetSubRollNum(dwMaster["ABPAUFPOS"].ToString()));
            //exAgent.SetCellValue(3, 15, dwABK["ABKKDAUFTNR"].ToString());
            exAgent.SetCellValue(3, 23, dwMaster["ABPFARBE"].ToString());
            exAgent.SetCellValue(4, 5, art);
            var qGS = from DataRow row in db.Tables["GSDAT"].AsEnumerable()
                      where row["GSART"].ToString() == art
                      && row["GSVAR"].ToString() ==var
                      select row;
            foreach (DataRow row in qGS)
            {
                exAgent.SetCellValue(4, 5, art.Trim()+":"+GetGB2312Value(row["GSHANDLNR"].ToString().Trim()));
            }
            exAgent.SetCellValue(4, 21, color);
            var qGC = from DataRow row in db.Tables["GSCOLOR"].AsEnumerable()
                      where row["GSCART"].ToString() == art
                      && row["GSCVAR"].ToString() == var
                      && row["GSCFARBE"].ToString() ==color
                      select row;
            foreach (DataRow row in qGC)
            {
                exAgent.SetCellValue(4, 21, color.Trim() + ":" + GetGB2312Value(row["GSCBEZ"].ToString().Trim()));
                break;
            }
            var qM = from DataRow row in db.Tables["GSMATK"].AsEnumerable()
                  where row["GSMART"].ToString() == art
                  && row["GSMVAR"].ToString() == var
                  select row;
            string composition = "";
            foreach (DataRow row in qM)
            {
                composition += row["GSMRBEZ"].ToString().Trim() + Convert.ToInt16(row["GSMRANT"].ToString().Trim()) / 10 + "%";
            }
            exAgent.SetCellValue(5, 15, composition);
            exAgent.SetCellValue(5, 5, Convert.ToDouble(dwMaster["ABPSOLLMENGE5"].ToString()) / 100);
            exAgent.SetCellValue(5, 29, dwMaster["ABPLIEFDAT"].ToString());
            exAgent.SetCellValue(8, 5, dwMaster["ABPFILLER1"].ToString());
            #endregion
            string 条码 = "";
            string 客单 = "";
            string ABAPOS = "";
            var query = from DataRow dwRow in db.Tables["ABAUFM"].AsEnumerable()
                    where
                     dwRow["ABAAUFKRS"].ToString() == KRS
                    && dwRow["ABAAUFLFD"].ToString() == LFD
                    && dwRow["ABAAUFPOS"].ToString() == POS
                    select dwRow;
            string processed = "";
            foreach (DataRow dwRow in query)
            {
                ABAPOS = dwRow["ABAAPOS"].ToString();
                if(processed.Contains(ABAPOS+";")) continue;
                processed += ABAPOS + ";";
                double ABARESMG = Convert.ToDouble(dwRow["ABARESMG"].ToString()) * 0.001;
                double ABARSTSGEW = Convert.ToDouble(dwRow["ABARSTSGEW"].ToString()) * 0.001;

                if (ABARESMG == 0 && ABARSTSGEW == 0) continue;
             
                条码 = "*" + dwRow["ABAAUFKRS"].ToString() + dwRow["ABAAUFLFD"].ToString() + dwRow["ABAAUFPOS"].ToString() + Helper.Right(dwRow["ABAAPOS"].ToString(), 2) + "*";
                exAgent.SetCellValue(8 + idx, 5, 条码);
                客单 = dwRow["ABAVKAUFKRS"].ToString() + "-" + dwRow["ABAVKAUFLFD"].ToString() + "-" + dwRow["ABAVKAUFPOS"].ToString();
                exAgent.SetCellValue(8 + idx, 21, 客单);
                exAgent.SetCellValue(8 + idx, 33, ABARESMG); 
                idx++;
            }
            #region====yarn=========
            rStart = 16;
            nRow = 2;
            nPRow = 2;
            idx = 0;
            int ycount = 0;
            /*
           ycount = db.Tables["GAAUF"].Rows.Count;
         foreach (DataRow ga in db.Tables["GAAUF"].Rows)
         {
             if (ga["GASA"].ToString() == "310")
             {
                 ycount--;
                 continue;
             }
             string gaart = ga["GAART"].ToString();
             string gavar = ga["GAVAR"].ToString();
             string gacolor = ga["GAFARBE"].ToString();

             double gakg1 = System.Convert.ToDouble(ga["GAKG1"].ToString()) * 0.01;
             double gakg5 = System.Convert.ToDouble(ga["GAKG5"].ToString()) * 0.01;
             string yarn = gaart.Trim();
             if (gavar.Trim() != "")
                 yarn += "/" + gavar.Trim();
             if (gacolor.Trim() != "")
             {
                 if (gacolor.Trim() != "000000")
                 {
                     yarn += "/" + gacolor.Trim();
                 }
             }

             exAgent.SetCellValue(rStart + idx * nRow, 3, yarn);
             exAgent.SetCellValue(rStart + idx * nRow, 24, gakg1);
             //exAgent.SetCellValue(rStart + idx * nRow, 33, gakg5);
             #region=====Alloction=======
             if (db.Tables.Contains("ZUTEI"))
             {
                 var qzu = from rows in db.Tables["ZUTEI"].AsEnumerable()
                           where rows["ZUAUFKRS"].ToString() == ga["GAAUFKRS"].ToString()
                              & rows["ZUAUFLFD"].ToString() == ga["GAAUFLFD"].ToString()
                             & rows["ZUAUFPOS"].ToString() == ga["GAAUFPOS"].ToString()
                              & rows["ZUSA"].ToString() == ga["GASA"].ToString()
                              & rows["ZUPOS"].ToString() == ga["GAPOS"].ToString()
                           select rows;
                 string filters = "";
                 DataRow[] rx;
                 DataRow dwPartie = null;
                 string info = "";
                 int zucount = qzu.Count();
                 int zupos = 0;
                 foreach (DataRow rw in qzu)
                 {
                     // ZUPARTIEZULO	ZULB ZUEINTKG ZUENTNKG
                     double allocated = System.Convert.ToDouble(rw["ZUEINTKG"].ToString().Trim()) * 0.001;
                     double get = System.Convert.ToDouble(rw["ZUENTNKG"].ToString().Trim()) * 0.001;
                     double TOGET = allocated - get;
                     string sppartie = rw["ZUSPPART"].ToString().Trim();
                     exAgent.SetCellValue(rStart + idx * nRow, 27, GetGB2312Value(sppartie));
                     exAgent.SetCellValue(rStart + idx * nRow, 33, TOGET);
                 }
             }
             #endregion
             idx++;
             if (idx < ycount)
             {
                 exAgent.RangeCopy(16, 17, rStart + idx * nRow, rStart + idx * nRow + 1);
             }
         }  */
            #endregion

            #region======Step=========
            rStart = 61;
            nRow = 2;
            nPRow =1;
            idx = 0;
            ycount = db.Tables["ABFOLGE"].Rows.Count;
            foreach (DataRow row in db.Tables["ABFOLGE"].Rows)
            {
                exAgent.SetCellValue(rStart + idx * nRow, 1, "*"
                    + row["ABFAUFKRS"].ToString()
                    + row["ABFAUFLFD"].ToString() 
                    + row["ABFAUFPOS"].ToString()
                    + row["ABFAFOLGE"].ToString() 
                    + "*");//barcode
                exAgent.SetCellValue(rStart + idx * nRow, 11, GetGB2312Value(row["ABFAGANG"].ToString().Trim() + row["ABFBZAGANG"].ToString().Trim()));
                exAgent.SetCellValue(rStart + idx * nRow, 15, row["ABFSOLLSTART"].ToString().Substring(4,4));
                if (row["ABFMLFDNR"].ToString() != "0000")
                {
                    exAgent.SetCellValue(rStart + idx * nRow, 18, row["ABFMNRKRS"].ToString().Trim()
                        + "-" + Helpers.Helper.GetSubRollNum(row["ABFMLFDNR"].ToString())
                        + "-" + Helpers.Helper.GetSubRollNum(row["ABFMPOS"].ToString()));
                }
                idx++;
                if (idx < ycount)
                {
                    exAgent.RangeCopy(61, 62, rStart + idx * nRow, rStart + idx * nRow + 1);
                }
            }
            #endregion
     
            #region=========footer===========
            exAgent.SetCellValue(rStart + idx * nRow + 3, 2, "制表人：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 2, "日期：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 3, 10, "主管：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 10, "日期：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 3, 23, "经理：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 23, "日期：			_____________");
            #endregion
        } 
    }
}